package com.atguigu.edu.util;

import org.wltea.analyzer.core.IKSegmenter;
import org.wltea.analyzer.core.Lexeme;

import java.io.IOException;
import java.io.StringReader;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;

public class AtguiguUtil {
    private static final DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
    private static final DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
    
    public static String tsToDate(Long ts) {
        return dateFormatter.format(LocalDateTime.ofInstant(new Date(ts).toInstant(), ZoneId.systemDefault()));
    }
    
    public static String tsToDateTime(long ts) {
        return dateTimeFormatter.format(LocalDateTime.ofInstant(new Date(ts).toInstant(), ZoneId.systemDefault()));
    }
    
    // 把年月日变成毫秒值
    public static Long dateToTs(String date) throws ParseException {
        return new SimpleDateFormat("yyyy-MM-dd").parse(date).getTime();
    }
    //把年月日时分秒变成毫秒值
    // 把年月日变成毫秒值
    public static Long dateTimeToTs(String date) throws ParseException {
        return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(date).getTime();
    }

    
    // 使用 ik 分词对传入的字符串进行分词
    public static Set<String> ikSplit(String item) {
        Set<String> set = new HashSet<>();
        // String  ->  Reader   // FileReader FileWriter
        StringReader reader = new StringReader(item);
        // true: 智能模式  false: max_word
        IKSegmenter ikSegmenter = new IKSegmenter(reader, false);
        Lexeme next = null;
        try {
            next = ikSegmenter.next();
            while (next != null) {
                String kw = next.getLexemeText();
                set.add(kw);
                next = ikSegmenter.next();
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        
        return set;
    }
    
    
}
